/*
 * Copyright (C) 2008-2009 Cristian Olaru <colaru@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *         http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// this are the scripts for the entire mengine - separated from js libraries and frameworks used in this application
//mengine/src/main/webapp/WEB-INF/pages/production/articleContent.jsp
function submitCommentForm() {
	removeArea2();
	$("#commentForm").submit();
}

function resetCommentForm() {
	$("#commentForm").reset();
}

// /mengine/src/main/webapp/WEB-INF/pages/production/articleContentForm.jsp
function submitArticleForm() {
	removeArea2();
	$("#articleForm").submit();
}

function previewArticle() {
	$("#articleForm").submit();
}

// /mengine/src/main/webapp/WEB-INF/pages/production/userProfileForm.jsp
function submitUserProfileForm() {
	$("#profileForm").submit();
}

function resetUserProfileForm() {
	$("#profileForm").reset();
}

// /mengine/src/main/webapp/include/logo.jsp

function swichForPass() {
	if ($("#username").val() == "")
		$("#username").val("user id");
	$("#password").val("");
}

function swichForUser() {
	$("#username").val("");
	if ($("#password").val() == "")
		$("#password").val("user id");
}

$(document).ready( function() {
	$("#username").val("user id");
	$("#password").val("user id");
});

// /mengine/src/main/webapp/include/search.jsp

function submitSearchForm() {
	var sv = $("#query").val();
	$("#clientQuery").val(sv);
	$("#searchForm").action = "searchArticles.html";
	$("#searchForm").submit();
}

function submitNewsletterForm() {
	$("#MERGE0").val($("#emailAdress").val());
	$("#MERGE1").val($("#firstName").val());
	$("#MERGE2").val($("#lastName").val());
	$("#newsletterForm").submit();
}

function checkEnterForSearch(e){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
	e = e
	characterCode = e.which //character code is contained in NN4's which property
	}
	else{
	e = event
	characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		submitSearchForm();
	return false
	}
	else{
	return true
	}
}

// /mengine/src/main/webapp/include/logo.jsp

function submitLoginForm() {
	var user = $("#username").val();
	$("#j_username").val(user);
	var pass = $("#password").val();
	$("#j_password").val(pass);
	$("#loginForm").action = "/j_security_check";
	$("#loginForm").submit();
}

function checkEnterForLogin(e){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
	e = e
	characterCode = e.which //character code is contained in NN4's which property
	}
	else{
	e = event
	characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		submitLoginForm();
	return false
	}
	else{
	return true
	}
}
// /mengine/src/main/webapp/WEB-INF/pages/admin/searchIndex.jsp

function submitQueryForm() {
	$("#searchQueryForm").submit();
}

// /mengine/src/main/webapp/WEB-INF/pages/admin/categoryForm.jsp
function submitCategoryForm() {
	$("#categoryForm").submit();
}

///mengine/src/main/webapp/WEB-INF/pages/admin/aprouveArticlesList.jsp

var enabled='<img alt="ENABLED" src="images/emblem-system.png">';
var disabled='<img alt="DISABLED" src="images/emblem-unreadable.png">'

///mengine/src/main/webapp/WEB-INF/pages/production/articleContentForm.jsp
// Remember to invoke within jQuery(window).load(...)
// If you don't, Jcrop may not initialize properly
jQuery(window).load(function(){

	jQuery('#cropbox').Jcrop({
		onChange: showPreview,
		onSelect: showPreview,
		aspectRatio: 224 / 152
	});

});

// Our simple event handler, called from onChange and onSelect
// event handlers, as per the Jcrop invocation above
function showPreview(coords)
{
	var rx = 224 / coords.w;
	var ry = 152 / coords.h;
	
	$("#articleForm_x").val(coords.x);
	$("#articleForm_y").val(coords.y);
	$("#articleForm_w").val(coords.w);
	$("#articleForm_h").val(coords.h);

	jQuery('#preview').css({
		width: Math.round(rx * 500) + 'px',
		height: Math.round(ry * 370) + 'px',
		marginLeft: '-' + Math.round(rx * coords.x) + 'px',
		marginTop: '-' + Math.round(ry * coords.y) + 'px'
	});
}

// /mengine/src/main/webapp/WEB-INF/pages/admin/linkForm.jsp

function submitLinkForm() {
	$("#linkForm").submit();
}

function resetLinkForm() {
	$("#linkForm").attr("action","/links.html");
	$("#linkForm").submit();
}

// /mengine/src/main/webapp/WEB-INF/pages/admin/applicationConfiguration.jsp

function submitConfigurationForm() {
	$("#configurationForm").submit();
}

function resetConfigurationForm() {
	$("#configurationForm").attr("action","/applicationConfiguration.html");
	$("#configurationForm").submit();
}
